addpath('/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/ITIanalysis/Scripts/Analyses/ROI_Analysis');

subjectList = {...
    'D121412K'...
    'M121412M'...
    };

plotheight = 50;
xlimits = 150;

subjectsDir = '/Users/marcelomattar/Data/ITIanalysis/Subjects/';
numSubjects = length(subjectList);
distributionOfBetas_noTD_1back = cell(1,numSubjects);
distributionOfBetas_noTD_1to6back = cell(1,numSubjects);
distributionOfBetas_TD_1back = cell(1,numSubjects);
distributionOfBetas_TD_1to6back = cell(1,numSubjects);


for subjIndx = 1:numSubjects
    [averageBetas , maskedBetas, averageBetasMatrix stdBetasMatrix] = extractROImask(subjectList{subjIndx}, subjectsDir);
    
    ITIs = zeros(size(averageBetasMatrix,1),1);
    fullModel = zeros(size(averageBetasMatrix,1),1);
    tempDeriv = zeros(size(averageBetasMatrix,1),1);
    for i=1:size(averageBetasMatrix,1)
        ITIs(i) = str2double(averageBetas{i+1,1});
        fullModel(i) = strcmp(averageBetas{i+1,2},'1-6');
        tempDeriv(i) = averageBetas{i+1,3};
    end
    
    distributionOfBetas_noTD_1back{1,subjIndx} = maskedBetas(and(~fullModel,~logical(tempDeriv)),:);
    [~,I] = sort(ITIs(and(~fullModel,~logical(tempDeriv)))); 
    distributionOfBetas_noTD_1back{1,subjIndx} = distributionOfBetas_noTD_1back{1,subjIndx}(I,:);
    
    distributionOfBetas_noTD_1to6back{1,subjIndx} = maskedBetas(and(fullModel,~logical(tempDeriv)),:);
    [~,I] = sort(ITIs(and(fullModel,~logical(tempDeriv)))); 
    distributionOfBetas_noTD_1to6back{1,subjIndx} = distributionOfBetas_noTD_1to6back{1,subjIndx}(I,:);
    
    distributionOfBetas_TD_1back{1,subjIndx} = maskedBetas(and(~fullModel,logical(tempDeriv)),:);
    [~,I] = sort(ITIs(and(~fullModel,logical(tempDeriv)))); 
    distributionOfBetas_TD_1back{1,subjIndx} = distributionOfBetas_TD_1back{1,subjIndx}(I,:);
    
    distributionOfBetas_TD_1to6back{1,subjIndx} = maskedBetas(and(fullModel,logical(tempDeriv)),:);
    [~,I] = sort(ITIs(and(fullModel,logical(tempDeriv)))); 
    distributionOfBetas_TD_1to6back{1,subjIndx} = distributionOfBetas_TD_1to6back{1,subjIndx}(I,:);
    
end


figure(1);
subplot(3,4,1);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,1});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,1});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,2);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,1});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,1});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,3);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,1});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,1});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,4);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,1});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,1});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,5);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,2});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,2});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,6);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,2});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,2});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,7);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,2});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,2});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,8);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,2});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,2});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,9);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,3});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{1,3});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,10);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,3});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{2,3});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,11);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,3});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{3,3});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
subplot(3,4,12);
h = histfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,3});
    axis([-xlimits xlimits 0 plotheight]);
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas_TD_1to6back{1,subjIndx}{4,3});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);






for subjIndx = 1:numSubjects
    subplot(1,numSubjects,subjIndx);
    %hist(distributionOfBetas{1,subjIndx},-140:10:150);
    h = histfit(distributionOfBetas{1,subjIndx});
    axis([-xlimits xlimits 0 plotheight])
    title(subjectList{subjIndx});
    set(h(1),'facecolor',[0.5 0.5 0.5]);
    set(h(2),'linewidth',2);
    [muhat,sigmahat] = normfit(distributionOfBetas{1,subjIndx});
    hold on;
    plot([muhat-2*sigmahat muhat-2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat+2*sigmahat muhat+2*sigmahat],[0 plotheight],'b','linewidth',1);
    plot([muhat muhat],[0 plotheight],'b','linewidth',2);
end